/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hnzq.cloud.nacosdiscovery;

import com.hnzq.cloud.api.BalanceApi;
import com.hnzq.cloud.balanceParam.CountBalanceParam;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@EnableDiscoveryClient
@Configuration
public class NacosDiscoveryConfiguration {
    //在集群的情况下，一定要加分布式锁，防止高并发
    @Resource
    private BalanceApi balanceApi;
    @Scheduled(cron = "1 * * * * *")
//    @Scheduled(cron = "0 15 10 * L ?")
    public void doBalance(){
        Date sDate = new Date();
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        String endDate = f.format(sDate);
        Calendar c = Calendar.getInstance();
        c.setTime(sDate);
        c.set(Calendar.DAY_OF_MONTH, 1);//利用Calendar 实现 Date日期1号
        sDate = c.getTime();
        String startDate = f.format(sDate);
        //startDate是当月1号 endDate是当前时间
        CountBalanceParam countBalanceParam = new CountBalanceParam();
        countBalanceParam.setStartTime(startDate);
        countBalanceParam.setEndTime(endDate);
        balanceApi.countBalanceBySiteId(countBalanceParam);
    }
}
